<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button
          type="primary"
          v-if="hasPermission(['sysDatasource:add'])"
          @click="handleCreate"
        >
          {{ t('新增') }}
        </a-button>
        <a-button
          type="success"
          :loading="testLoading"
          v-if="testShow && hasPermission(['sysDatasource:test'])"
          @click="handleTest"
        >
          {{ t('测试连接') }}
        </a-button>
        <a-button
          @click="handleExport"
          v-if="hasPermission(['sysDatasource:export'])"
          preIcon="ant-design:file-excel-outlined"
          :loading="exportLoading"
        >
          {{ t('导出') }}
        </a-button>
      </template>

      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              auth: 'sysDatasource:edit',
              tooltip: t('编辑'),
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              auth: 'sysDatasource:delete',
              color: 'error',
              tooltip: t('删除'),
              popConfirm: {
                title: t('是否确认删除'),
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <EditModal ref="modal" @register="registerModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
export default defineComponent({
  name: 'SysDatasource',
});
</script>
<script lang="ts" setup>
import { ref, computed } from 'vue';
import { usePermission } from '@/hooks/web/usePermission';
import { BasicTable, useTable, TableAction } from '@/components/Table';
import { page, exp, del } from '@/api/sys/dev/datasourceManager';
import { useModal } from '@/components/Modal';
import EditModal from './EditModal.vue';
import { useMessage } from '@/hooks/web/useMessage';
import { columns, searchFormSchema } from './data';
import { useI18n } from '@/hooks/web/useI18n';
import { defHttp } from '@/utils/http/axios';

const { t } = useI18n();
const { createMessage } = useMessage();
const [registerModal, { openModal }] = useModal();
const { hasPermission } = usePermission();
const [registerTable, { reload, getForm, getSelectRows }] = useTable({
  title: t('列表'),
  api: page,
  columns,
  rowKey: 'id',
  formConfig: {
    labelWidth: 120,
    schemas: searchFormSchema,
    baseColProps: { span: 6 },
  },
  useSearchForm: true,
  rowSelection: {
    type: 'radio',
  },
  pagination: true,
  bordered: true,
  showIndexColumn: false,
  actionColumn: {
    width: 120,
    title: t('操作'),
    dataIndex: 'action',
    slots: { customRender: 'action' },
    fixed: 'right',
  },
});
function handleCreate() {
  openModal(true, {
    isUpdate: false,
  });
}
function handleEdit(record: Recordable) {
  openModal(true, {
    record,
    isUpdate: true,
  });
}
function handleDelete(record: Recordable) {
  del('reportDataSource', [record.id]).then(() => {
    createMessage.success(t('删除成功'));
    reload();
  });
}
function handleTest() {
  const record = getSelectRows();
  testLoading.value = true;
  defHttp
    .get({ url: `/reportDataSource/test/${record[0].id}` })
    ?.then(() => {
      testLoading.value = false;
      createMessage.success(t('测试成功'));
    })
    .catch(() => (testLoading.value = false));
}
function handleSuccess() {
  reload();
}
const testLoading = ref(false);
const testShow = computed(() => {
  const selected = getSelectRows();
  if (selected && selected.length) {
    return true;
  }
  return false;
});

let exportLoading = ref<Boolean>(false);
const handleExport = () => {
  exportLoading.value = true;

  exp('reportDataSource', getForm().getFieldsValue())
    .then(() => {
      exportLoading.value = false;
    })
    .catch(() => {
      exportLoading.value = false;
    });
};
</script>
